﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TamTreSoSinh.DBContext;

namespace TamTreSoSinh.Admin
{
    public partial class SettingServiceCategoryDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            int Id;
            bool IsValidId = int.TryParse(Request["Id"], out Id);
            if (IsValidId)
            {
                LoadData(Id);
            }
            else
            {
                // prepare create new
                txtPriority.Text = "1";
            }
        }

        private void LoadData(int Id)
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();
            ServiceCategory serviceCategory = dbContext.ServiceCategories.FirstOrDefault(obj => obj.Id == Id);

            if (serviceCategory == null)
            {
                return;
            }

            txtPriority.Text = serviceCategory.Priority.ToString();
            txtTitle.Text = serviceCategory.Title;
            ddlVisible.SelectedValue = serviceCategory.IsVisible.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!IsValid)
            {
                return;
            }

            if (string.IsNullOrEmpty(Request["Id"]))
            {
                ProcessInsert();
            }
            else
            {
                ProcessUpdate();
            }

            Response.Redirect(@"/Admin/SettingServiceCategory.aspx");
        }

        private void ProcessUpdate()
        {
            int Id;
            bool IsValidId = int.TryParse(Request["Id"], out Id);

            if (!IsValidId)
            {
                return;
            }

            DBTamTreEntities dbContext = new DBTamTreEntities();
            ServiceCategory serviceCategory = dbContext.ServiceCategories.FirstOrDefault(obj => obj.Id == Id);

            if (serviceCategory == null)
            {
                return;
            }

            serviceCategory.Priority = Convert.ToInt32(txtPriority.Text);
            serviceCategory.Title = txtTitle.Text;
            serviceCategory.IsVisible = Convert.ToBoolean(ddlVisible.SelectedValue);

            dbContext.SaveChanges();
        }

        private void ProcessInsert()
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();
            ServiceCategory serviceCategory = new ServiceCategory();
            serviceCategory.Priority = Convert.ToInt32(txtPriority.Text);
            serviceCategory.Title = txtTitle.Text;
            serviceCategory.IsVisible = Convert.ToBoolean(ddlVisible.SelectedValue);

            dbContext.ServiceCategories.AddObject(serviceCategory);
            dbContext.SaveChanges();
        }
    }
}